package com.yanqu.road.dao.impl.activity.area;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.area.wase.ChannelWashePkUserData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WashePkUserDataDaoImpl extends TempDataObjectDao<ChannelWashePkUserData> {

    public static String getChannelWashePkUserDataSql = "select * from `t_n_activity_washe_pk_user_data` where `activityId`=? ";
    public Map<Long, ChannelWashePkUserData> getChannelWashePkUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, ChannelWashePkUserData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getChannelWashePkUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelWashePkUserData userData = parse(rs);
                result.put(userData.getUserId(), userData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateChannelWashePkUserDataSql = "update `t_n_activity_washe_pk_user_data` set userBaseInfo=?,score=?,maxWinTimes=?,winTimes=?,badge=?,lastUpdateTime=?,lastUpdateTime2=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ChannelWashePkUserData channelWashePkUserData) {
        channelWashePkUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(channelWashePkUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, channelWashePkUserData.getMaxWinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, channelWashePkUserData.getWinTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getBadge()));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getLastUpdateTime2()));
        para.put(i++, new DbParameter(Types.INTEGER, channelWashePkUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getUserId()));
        if (getDb().executeNoneQuery(updateChannelWashePkUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChannelWashePkUserDataSql = "insert into `t_n_activity_washe_pk_user_data`(activityId,userId,userBaseInfo,score,maxWinTimes,winTimes,badge,lastUpdateTime,lastUpdateTime2) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ChannelWashePkUserData channelWashePkUserData) {
        channelWashePkUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, channelWashePkUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(channelWashePkUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, channelWashePkUserData.getMaxWinTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, channelWashePkUserData.getWinTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getBadge()));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, channelWashePkUserData.getLastUpdateTime2()));
        if (getDb().executeNoneQuery(insertChannelWashePkUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ChannelWashePkUserData parseData(ResultSet rs) throws SQLException {
        ChannelWashePkUserData channelWashePkUserData = new ChannelWashePkUserData();
        channelWashePkUserData.setActivityId(rs.getInt("activityId"));
        channelWashePkUserData.setUserId(rs.getLong("userId"));
        channelWashePkUserData.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        channelWashePkUserData.setScore(rs.getLong("score"));
        channelWashePkUserData.setMaxWinTimes(rs.getInt("maxWinTimes"));
        channelWashePkUserData.setWinTimes(rs.getInt("winTimes"));
        channelWashePkUserData.setBadge(rs.getLong("badge"));
        channelWashePkUserData.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        channelWashePkUserData.setLastUpdateTime2(rs.getLong("lastUpdateTime2"));

        return channelWashePkUserData;
    }
}
